package best.driver.android.Activity.Order;


import android.os.Bundle;
import best.driver.android.Activity.Base.BaseActivity;
import best.driver.android.Activity.Order.Adapter.OrderItemAdapter;
import best.driver.android.Activity.Order.Adapter.ValidOrderItemAdapter;
import best.driver.android.AppInfo;
import best.driver.android.Beans.NewOrderInfo;
import best.driver.android.Beans.OrderListInfo;
import best.driver.android.Beans.ValidOrderItem;
import best.driver.android.BestApp;
import best.driver.android.Event.NewOrderEvent;
import best.driver.android.R;
import best.driver.android.RequestFactory;
import best.driver.android.View.BasePullToRefreshListView;
import best.driver.android.common.app.AppProxyFactory;
import best.driver.android.common.network.ApiArrayRequest;
import best.driver.android.common.network.ApiObjectRequest;
import best.driver.android.common.network.ApiRequest;
import best.driver.android.common.network.BestError;
import butterknife.ButterKnife;
import butterknife.InjectView;
import de.greenrobot.event.EventBus;

import java.util.ArrayList;
import java.util.List;

public class ValidOrderListActivity extends BaseActivity implements BasePullToRefreshListView.OnPullDownListener{

    private int mPage = 1;

    private final static int PAGE_SIZE = 20;

    boolean mHasMore = true;

    @InjectView(R.id.prv_order_list)
    BasePullToRefreshListView mPrvOrders;

    ValidOrderItemAdapter mAdapter;

    List<NewOrderInfo> mItems = new ArrayList<NewOrderInfo>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_order_list);
        ButterKnife.inject(this, view);
        mPrvOrders.setOnPullDownListener(this);
        mAdapter = new ValidOrderItemAdapter(this);
        mPrvOrders.setAdapter(mAdapter);
        mAdapter.setData(mItems);
        mPrvOrders.setRefreshEnabled(true);
        setTopTopic("附近订单");
        setLeftIvBak(R.drawable.icon_navibar_back);
        getOrders(mPage, PAGE_SIZE, true);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onRefresh() {
        getOrders(1, PAGE_SIZE, false);
    }

    @Override
    public void onMore() {
//        if(mHasMore) {
//            mHandler.postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    getOrders(mPage, PAGE_SIZE, false);
//                }
//            }, 100);
//
//        }
    }

    private void getOrders(final int page, int pageSize, final boolean isFirst) {
        if(isFirst) {
            showWaiting();
        }
        String locationString = BestApp.getInstance().lastKnowLocation.mLng + "," + BestApp.getInstance().lastKnowLocation.mLat;
        ApiArrayRequest<ValidOrderItem> request = RequestFactory.createGetValidOrderListRequest(locationString);
        request.setListener(new ApiRequest.ApiRequestListener<ArrayList<ValidOrderItem>>() {
            @Override
            public void onRequestSuccess(ArrayList<ValidOrderItem> response) {
                List<NewOrderInfo> infos = new ArrayList<NewOrderInfo>();
                for(int i = 0; i < response.size(); i++) {
                    if(!response.get(i).chooseFlag) {
                        infos.add(response.get(i).orderInfo);
                    }
                }
                mAdapter.setData(infos);
                hideWaiting();
                mPrvOrders.onRefreshComplete();
            }

            @Override
            public void onRequestError(BestError error) {
                hideWaiting();
                if(BestApp.getInstance().getLastActivity() instanceof ValidOrderListActivity) {
                    AppInfo.showToast(ValidOrderListActivity.this, "网络加载失败，请检查网络或下拉刷新");
                }
                mPrvOrders.onRefreshComplete();
            }
        });
        AppProxyFactory.getProxy().getNetworkManager().enqueueRequest(request);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onResume() {
        super.onResume();
        getOrders(mPage, PAGE_SIZE, false);
    }

    public void onEventMainThread(NewOrderEvent event) {
        getOrders(0, 0 , false);
    }

}
